﻿/*
' DotNetNuke® - http://www.dotnetnuke.com
' Copyright (c) 2002-2009
' by DotNetNuke Corporation
'
' Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
' documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
' the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
' to permit persons to whom the Software is furnished to do so, subject to the following conditions:
'
' The above copyright notice and this permission notice shall be included in all copies or substantial portions 
' of the Software.
'
' THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
' TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
' THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
' CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
' DEALINGS IN THE SOFTWARE.
*/

using System;
using MbUnit.Framework;
using WatiN.Core;
using DotNetNuke.Tests.UI.WatiN.Common;
using Gallio.Framework;
using DotNetNuke.Tests.UI.WatiN.Utilities;
using System.Configuration;
using WatiN.Core.DialogHandlers;
using DotNetNuke.Tests.UI.WatiN.Common.WatiNObjects;
using System.IO;
using System.Reflection;




namespace DotNetNuke.Tests.UI.WatiN.AdministratorTests
{
    [TestFixture]

    public class AdminExtensionsTests : ATBase
    {
        public bool LanguagePackInstalled = false;
        public bool ExtLanguagePackInstalled = false;

        #region Test Setup and TearDown

        [SetUp]
        public void SetUp()
        {

            SetUpTest();
        }

        [TearDown]
        public void TearDown()
        {
            if (LanguagePackInstalled)
            {
                WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
                LoginPage loginPage = new LoginPage(homePage);
                ieInstance.GoTo(SiteURL);
                if (!homePage.RegisterLink.Text.Contains(TestUsers.Host.DisplayName))
                {
                    if (!homePage.RegisterLink.Text.Equals("Register"))
                    {
                        loginPage.LogoffUser();
                        System.Threading.Thread.Sleep(1500);

                    }
                    loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
                }
                //Uninstall the language pack and delete the files
                
                ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
                if (ExtLanguagePackInstalled)
                {
                    ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                    //extensionsPage.ExtensionPackageSelectList.Select("Extension Language Pack");
                    System.Threading.Thread.Sleep(2000);
                    extensionsPage.GetExtensionUninstallButton(LocaleName, extensionsPage.ExtensionLanguagePacksTable).Click();
                    System.Threading.Thread.Sleep(2000);
                    extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                    ConfirmDialogHandler extDialog = new ConfirmDialogHandler();
                    using (new UseDialogOnce(ieInstance.DialogWatcher, extDialog))
                    {
                        extensionsPage.UninstallPackageLink.ClickNoWait();
                        extDialog.WaitUntilExists();
                        extDialog.OKButton.Click();
                    }
                    ieInstance.WaitForComplete();
                }
                ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
                ExtLanguagePackInstalled = false;
                //extensionsPage.ExtensionPackageSelectList.Select("Core Language Pack");
                //extensionsPage.CoreLanguagePackSectionLink.Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.GetExtensionUninstallButton(LocaleName, extensionsPage.CoreLanguagePacksTable).Click();
                System.Threading.Thread.Sleep(2000);
                extensionsPage.DeleteExtensionFilesCheckbox.Checked = true;
                ConfirmDialogHandler dialog = new ConfirmDialogHandler();
                using (new UseDialogOnce(ieInstance.DialogWatcher, dialog))
                {
                    extensionsPage.UninstallPackageLink.ClickNoWait();
                    dialog.WaitUntilExists();
                    dialog.OKButton.Click();
                }
                ieInstance.WaitForComplete();
                LanguagePackInstalled = false;
            }
            TearDownTest();
        }

        #endregion

        #region Tests

        [Test]
        public void Edit_Authentication_System_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            //extensions.ExtensionPackageSelectList.Select("Authentication System");
            System.Threading.Thread.Sleep(1500);
            extensions.GetExtensionEditButton("Default Authentication", extensions.AuthenticationSystemsTable).Click();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Authentication Settings"));
            extensions.PackageSettingsSectionLink.Click();
            System.Threading.Thread.Sleep(1500);
            Assert.IsTrue(extensions.PackageNameSpan.InnerHtml.Contains("DefaultAuthentication"));
            extensions.PackageSettingsSectionLink.Click();
        }

        [Test]
        public void Edit_Container_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            //extensions.ExtensionPackageSelectList.Select("Container");
            extensions.ContainersSectionLink.Click();
            extensions.GetExtensionEditButton("Minimal Extropy Container", extensions.ContainersTable).ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Container Package Settings"));
            extensions.PackageSettingsSectionLink.Click();
            Assert.IsTrue(extensions.PackageNameSpan.InnerHtml.Contains("MinimalExtropy.Container"));
            extensions.PackageSettingsSectionLink.Click();
        }

        [Test]
        public void Edit_Core_Language_Pack_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            System.Threading.Thread.Sleep(1500);
            //extensions.ExtensionPackageSelectList.Select("Core Language Pack");
            extensions.CoreLanguagePackSectionLink.Click();
            extensions.GetExtensionEditButton(LocaleName, extensions.CoreLanguagePacksTable).ClickNoWait();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Language Pack Settings"));
            extensions.PackageSettingsSectionLink.Click();
            Assert.IsTrue(extensions.PackageNameSpan.InnerHtml.Contains(LocaleName));
            extensions.PackageSettingsSectionLink.Click();
        }

        [Test]
        public void Edit_Module_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            //extensions.ExtensionPackageSelectList.Select("Module");
            extensions.ModuleSectionLink.Click();
            extensions.GetExtensionEditButton("Languages", extensions.ModulesTable).Click();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Module Settings"));
            extensions.PackageSettingsSectionLink.Click();
            Assert.IsTrue(extensions.PackageNameSpan.InnerHtml.Contains("DotNetNuke.Languages"));
            extensions.PackageSettingsSectionLink.Click();
        }

        [Test]
        public void Edit_Extension_Language_Pack_Extensions()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            ExtensionsPage extensionsPage = new ExtensionsPage(homePage);
            LanguagePage languagePage = new LanguagePage(homePage);
            extensionsPage.InstallExtension(LanguagePath);
            ExtLanguagePackInstalled = true;
            extensionsPage.InstallExtension(ExtLanguagePath);
            LanguagePackInstalled = true;
            loginPage.LogoffUser();
            System.Threading.Thread.Sleep(1500);
            loginPage.LoginUser(TestUsers.Admin.UserName, TestUsers.Admin.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Admin/Extensions.aspx");
            //extensions.ExtensionPackageSelectList.Select("Extension Language Pack");
            extensions.ExtensionLanguagePacksSectionLink.Click();
            extensions.GetExtensionEditButton(LocaleName, extensions.ExtensionLanguagePacksTable).Click();
            System.Threading.Thread.Sleep(1000);

            //Assert
            Assert.IsTrue(homePage.ContentPane.InnerHtml.Contains("Language Pack Settings"));
            extensions.PackageSettingsSectionLink.Click();
            Assert.IsTrue(extensions.PackageNameSpan.InnerHtml.Contains("DNN_HTML " + LocaleName));
            extensions.PackageSettingsSectionLink.Click();
        }

        [Test]
        public void Install_Extension_Valid_Files()
        {
            //Arrange
            WatiNBase homePage = new WatiNBase(ieInstance, SiteURL, DatabaseName);
            LoginPage loginPage = new LoginPage(homePage);
            loginPage.LoginUser(TestUsers.Host.UserName, TestUsers.Host.Password);

            //Act
            ExtensionsPage extensions = new ExtensionsPage(homePage);
            ieInstance.GoTo(SiteURL + "/Host/Extensions.aspx");
            extensions.InstallExtension(LanguagePath);
            LanguagePackInstalled = true;

            //Assert 
           // extensions.CoreLanguagePackSectionLink.Click();
            Assert.IsTrue(extensions.CoreLanguagePacksTable.InnerHtml.Contains(LocaleName));
        }

        
        

        #endregion
    }
}